package com.evon.yardmanagement.service;

import java.util.ArrayList;
import java.util.List;

import com.evon.yardmanagement.dao.CarDAO;
import com.evon.yardmanagement.dao.KeyBoxDAO;
import com.evon.yardmanagement.dao.MemberDAO;
import com.evon.yardmanagement.dao.ParkingSlotDAO;
import com.evon.yardmanagement.dao.SectionDAO;
import com.evon.yardmanagement.model.Car;
import com.evon.yardmanagement.model.ParkingSlot;
import com.evon.yardmanagement.model.Section;
import com.evon.yardmanagement.model.Yard;
import com.evon.yardmanagement.security.YardSecurityManager;
import com.evon.yardmanagement.transformer.BaseTransformer;
import com.evon.yardmanagement.transformer.transaction.CarEntryTransformer;
import com.evon.yardmanagement.ui.dto.YardDTO;
import com.evon.yardmanagement.ui.dto.transaction.CarDTO;

public class CarManagementService {

	static CarDAO dao = new CarDAO();
	
	private static BaseTransformer<Car, CarDTO> transformer = new BaseTransformer<Car, CarDTO>(Car.class, CarDTO.class);

	public static Car findCar(String chasisNumber, int yardId) {
		List<Car> cars = dao.findCar(chasisNumber, yardId);
		if (cars != null && !cars.isEmpty()) {
			return cars.get(0);
		}
		return null;
	}

	public static Car findCar(int id) {
		List<Car> cars = dao.findByProperty("id", id);
		if (cars != null && !cars.isEmpty()) {
			return cars.get(0);
		}
		return null;
	}
	
	public static CarDTO findCarDTO(int id) {
		List<Car> cars = dao.findByProperty("id", id);
		if (cars != null && !cars.isEmpty()) {
			return transformer.transformToDTO(cars.get(0));
		}
		return null;
	}

	/**
	 * Find the available parking slot, driver, key bin and key box. First find
	 * the parking is permanent section, if not available then find in temporary
	 * section. Set the allocate items in the transient car.
	 * 
	 * @param transientCar
	 * @return
	 */
	public static List<ParkingSlot> allocateParking(int yardId) {
		return dao.getAvailableParkingForSection(yardId);
	}

	/**
	 * Mark the parking slot , driver and key box as occupied.
	 * 
	 * @param parkingSlotId
	 * @param memberId
	 * @param keyBoxId
	 */
	public void markSlotOccuppied(int parkingSlotId, int memberId, int keyBoxId) {
		new ParkingSlotDAO().markParkingSlotOccupied(parkingSlotId);
		new MemberDAO().markMemberOccupied(memberId);
		new KeyBoxDAO().markKeyBoxOccupied(keyBoxId);

	}

	/**
	 * Update the retrieval flag to 'Y' for the given car.
	 * 
	 * @param id
	 */
	public static CarDTO setForReteieval(int id, int retrievalDriverId) {

		 Car car = dao.markForRetrieval(id, retrievalDriverId);
		 new MemberDAO().markMemberOccupied(retrievalDriverId);
		 return transformer.transformToDTO(car);
	}

	public static Car confirmCarRetrieval(int id) {
		return dao.confirmCarRetrieval(id);

	}

	public static void setUnOccupied(int slotId, int keyBinId,
			int retrievalDriverId) {
		new ParkingSlotDAO().markParkingSlotUnOccupied(slotId);
		new KeyBoxDAO().markKeyBoxnUnOccupied(keyBinId);
		new MemberDAO().markMemberOccupied(retrievalDriverId);
	}

	/**
	 * Set the transporter details in the retrieved car.
	 * 
	 * @param id
	 * @param selectedTransporterId
	 */
	public static CarDTO assignTransporter(int id, int selectedTransporterId) {
		Car car = dao.assignTransporter(id, selectedTransporterId);
		if (car == null) {
			return null;
		}
		return CarEntryTransformer.transform(car);

	}

	/**
	 * Returns the list of sections in that yard TODO : Don't fetch the section
	 * which doesn't have any un occupied slots.
	 * 
	 */

	public static List<Section> getAvailableSections(int selectedYardId) {
		List<Section> sections = new SectionDAO()
				.getAvailableSections(selectedYardId);
		return sections;
	}

	public static List<YardDTO> getAvailableYards() {
		List<Yard> yardList = new ArrayList<Yard>(YardSecurityManager
				.getAccessibleYards());
		List<YardDTO> yardDTOList = new ArrayList<YardDTO>();
		if (yardList != null || !yardList.isEmpty()) {
			for (Yard yard : yardList) {
				YardDTO yardDTO = new YardDTO();
				yardDTO.setId(yard.getId());
				yardDTO.setName(yard.getName());
				yardDTOList.add(yardDTO);
			}
		}
		// TODO : Throwing lazy loading exception, need to figure out
		// return new BaseTransformer<Yard, YardDTO>(Yard.class,
		// YardDTO.class).transformToDTO(yardList);
		return yardDTOList;
	}
}
